//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2014 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// MultiLineHeaderGridCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "MultiLineHeaderGridCtrl.h"

//#include "BCGPGridExample.h"
#include "resource.h"       // main symbols
#include "MainFrm.h"
#include "MatchingCheckDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Custom Header

CRect CMultiLineHeaderGridCtrl::OnGetHeaderRect (CDC* pDC, const CRect& rectDraw)
{
	CRect rect = CBCGPGridCtrl::OnGetHeaderRect (pDC, rectDraw);
	rect.bottom = rect.top + rect.Height () * m_ColumnsEx.GetHeaderLineCount ();

	return rect;
}

void CMultiLineHeaderGridCtrl::OnDrawHeader (CDC* pDC)
{
	m_ColumnsEx.PrepareDrawHeader ();

	CBCGPGridCtrl::OnDrawHeader (pDC);
}

void CMultiLineHeaderGridCtrl::OnPrintHeader(CDC* pDC, CPrintInfo* pInfo)
{
	m_ColumnsEx.PreparePrintHeader ();

	CBCGPGridCtrl::OnPrintHeader(pDC, pInfo);
}

void CMultiLineHeaderGridCtrl::OnPosSizeChanged ()
{
	CBCGPGridCtrl::OnPosSizeChanged ();

	m_ColumnsEx.ReposHeaderItems ();
}

/////////////////////////////////////////////////////////////////////////////
// CMultiLineHeaderGridCtrl

IMPLEMENT_DYNCREATE(CMultiLineHeaderGridCtrl, CBCGPGridCtrl)

	CMultiLineHeaderGridCtrl::CMultiLineHeaderGridCtrl()
{
}

CMultiLineHeaderGridCtrl::~CMultiLineHeaderGridCtrl()
{
}

BEGIN_MESSAGE_MAP(CMultiLineHeaderGridCtrl, CBCGPGridCtrl)
	//{{AFX_MSG_MAP(CMultiLineHeaderGridCtrl)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Methods:

int CMultiLineHeaderGridCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CBCGPGridCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;

	EnableMarkSortedColumn (TRUE, FALSE);
	EnableHeader (TRUE, BCGP_GRID_HEADER_MOVE_ITEMS |BCGP_GRID_HEADER_SORT);
	SetReadOnly(TRUE);  
	SetSelectionBorder(TRUE);
	SetRowMarker(TRUE);
	EnableLineNumbers(TRUE);
	EnableDragHeaderItems(FALSE);
	SetVisualManagerColorTheme(FALSE, FALSE);
	SetClearInplaceEditOnEnter (FALSE);
	EnableRowHeader (TRUE);
	ShowWindow(SW_SHOW);

	return 0;
}

//********************************************************************************
void CMultiLineHeaderGridCtrl::OnDestroy() 
{
	CBCGPGridCtrl::OnDestroy();
}

void CMultiLineHeaderGridCtrl::SetHeaderLineCount( int nCount )
{

}

BOOL CMultiLineHeaderGridCtrl::AddHeaderItem( const CArray<int, int>* pCols,const CArray<int, int>* pLines, int nColumn,
	LPCTSTR lpszLabel, int nAlign, int iImage )
{
	return m_ColumnsEx.AddHeaderItem (pCols,pLines, nColumn, lpszLabel, nAlign, iImage);
}

void CMultiLineHeaderGridCtrl::AddHeaderItems( map<LPCTSTR,vector<int> > mapItemsInfo )
{
	m_ColumnsEx.SetHeaderLineCount (mapItemsInfo.size());
	map<LPCTSTR,vector<int> >::iterator iter= mapItemsInfo.begin();
	for (;iter!=mapItemsInfo.end();iter++)
	{
		vector<int> vecTemp=iter->second;
		CArray<int, int> arrMergeColumns;
		for (int n=0;n!=vecTemp.size();n++)
		{
			arrMergeColumns.Add (vecTemp[n]);
		}
		CArray<int, int> arrMergeLines;
		arrMergeLines.Add (0);
		m_ColumnsEx.AddHeaderItem (&arrMergeColumns, &arrMergeLines, -1/*1*/, iter->first, HDF_CENTER, 2);
	}
}

void CMultiLineHeaderGridCtrl::InsertColumns( vector<LPCTSTR> vecColumsInfo,int nColumnWidth )
{
	for (int n=0; n!=vecColumsInfo.size();n++)
	{
		InsertColumn (n, vecColumsInfo[n], nColumnWidth);
		SetHeaderAlign (n, HDF_CENTER);
		SetColumnAlign (n, HDF_CENTER);
	}
}

void CMultiLineHeaderGridCtrl::OnSelChanged( const CBCGPGridRange &range, BOOL bSelect )
{
	CBCGPGridCtrl::OnSelChanged(range,bSelect);
	CSSplitter* pSpli= dynamic_cast<CSSplitter*>(GetParent());
	CMatchingCheckDlg* pDlg= NULL;
	if (pSpli)
	{
		CWnd* pWnd=pSpli->GetParentOwner();
		CWnd* pTmep=pSpli->GetParent();
		pDlg= dynamic_cast<CMatchingCheckDlg*>(pSpli->GetParentOwner());
	}
	if (pDlg)
	{
		if (GetDlgCtrlID() == 100000)
		{
			pDlg->OnDeviceBaiscGridSelChanged();
		}
		else if (GetDlgCtrlID() == 100000+1)
		{
			pDlg->OnConnectorBaiscGridSelChanged();
		}
	}
}
